/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.TimeClockServer;

import TimeRecording.TimeClockServer.messages.ClockMessage;
import TimeRecording.TimeClockServer.database.ClockDatabase;
import TimeRecording.TimeClockServer.dtos.BadgeDetails;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * This class is used to build a download file for transmission to the clock
 * terminal.  Typically this is done when the communication is established with
 * the clock or when badge details have changed (badge details form part of the
 * download file).
 *
 * @author david
 */
public abstract class DownloadFile {

    private final static String HEADER_FILE = "/files/SetupHeader.dld";
    private final static String FOOTER_FILE = "/files/SetupFooter.dld";

    /**
     * This method will open a stream to the supplied clock command file and
     * pipe the commands found into an array list of ClockMessages which is
     * returned to the calling code.
     *
     *  1 The command file should be in the same JAR as the application
     *  2 Blank lines and comments (beginning with an '*') are ignored
     *
     * @param filename the path of the filename
     * @throws IOException
     */
    private static ArrayList<ClockMessage> processFile(
            String filename) throws IOException {

        ArrayList<ClockMessage> messages = new ArrayList<ClockMessage>();

        BufferedInputStream stream = new BufferedInputStream(
                DownloadFile.class.getResourceAsStream(filename));

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(stream));

        String message = reader.readLine();
        while (message != null) {
            message = message.trim();
            if (message.length() > 0 && message.charAt(0) != '*') {
                messages.add(new ClockMessage(message));
            }
            message = reader.readLine();
        }

        stream.close();
        return messages;
    }

    /**
     * This method extracts details of badges associated with employees from
     * the database and formulates these into the clock records necessary for
     * transmittal to the clocking terminal.
     *
     * @param database an friendly abstraction of the database
     * @return an array list of clock messages containing badge/employee details
     * @throws SQLException
     */
    private static ArrayList<ClockMessage> processBadges(
            ClockDatabase database) throws SQLException {

        ArrayList<ClockMessage> messages = new ArrayList<ClockMessage>();

        ArrayList<BadgeDetails> employees =
                database.getBadgeDetails();

        for (BadgeDetails employee : employees) {
            messages.add(employee.getInitialisationData());
        }

        return messages;
    }

    /**
     * This method performs the simple task of formulating an array list of
     * messages to be sent to a clocking terminal.  The messages describe clock
     * parameters, prompts and employee details.
     *
     * @param database an friendly abstraction of the database
     * @return an array list of commands to initialise the clocking terminal
     * @throws IOException
     * @throws SQLException
     */
    public static ArrayList<ClockMessage> buildDownloadFile(
            ClockDatabase database) throws IOException, SQLException {

        ArrayList<ClockMessage> messages = new ArrayList<ClockMessage>();
        messages.addAll(processFile(HEADER_FILE));
        messages.addAll(processBadges(database));
        messages.addAll(processFile(FOOTER_FILE));
        return messages;
    }
}
